#include<graphics.h>
#include<conio.h>

#define width 640//画布
#define high 480

void main()
{
	initgraph(width,high);
	int ballvx[15];
	int ballvy[15];
	for(int i=0;i<15;i++){
		ballvx[i]=1;
		ballvy[i]=1;
	}
	int radius=10;
	int ballx[15];
	int bally[15];
	ballx[0]=25;
	bally[0]=25;
	for(i=1;i<15;i++){
		ballx[i]=rand()%(width-100);
		bally[i]=rand()%(width-100);//小球初始值
			for(int j=0;j<i;j++){
			if(((ballx[i]-ballx[j])*(ballx[i]-ballx[j])+(bally[i]-bally[j])*(bally[i]-bally[j]))<=radius*2*radius*2){
				ballx[i]=ballx[i]+radius;
				bally[i]=bally[i]-radius;
				ballx[j]=ballx[j]-radius;
				bally[j]=bally[j]+radius;
			}
		}
	}
	BeginBatchDraw();

	while(1){
		for(i=0;i<15;i++){
			setcolor(YELLOW);
			setfillcolor(GREEN);
			fillcircle(ballx[i],bally[i],radius);
			
			setcolor(BLACK);
			setfillcolor(BLACK);
			fillcircle(ballx[i],bally[i],radius);

			ballx[i]=ballx[i]+ballvx[i];
			bally[i]=bally[i]+ballvy[i];

			setcolor(YELLOW);
			setfillcolor(GREEN);
			fillcircle(ballx[i],bally[i],radius);
			FlushBatchDraw();
			Sleep(0.5);
		
			
			if(ballx[i]==(width-radius)||ballx[i]==radius){
				ballvx[i]=-ballvx[i];
			}
			if(bally[i]==(high-radius)||bally[i]==radius){
				ballvy[i]=-ballvy[i];
			}//碰撞控制
			for(int j=0;j<15;j++){
				if(((ballx[i]-ballx[j])*(ballx[i]-ballx[j])+(bally[i]-bally[j])*(bally[i]-bally[j]))<=radius*2*radius*2){
					ballvx[i]=-ballvx[i];
					ballvy[i]=-ballvy[i];
					ballvx[j]=-ballvx[j];
					ballvy[j]=-ballvy[j];
				}
			}
		}
	
	}

	EndBatchDraw();
	closegraph();
}